<?PHP
class DailyPricesBO extends BusinessObject{
  /* private function using in constructors*/
  function setAttributes(){
    $this->fields   = array(
			"id"		 =>	"dpID",
			"item"		 =>	"dpItemID",
			"startdate"	 =>	"dpStartDate",
			"finishdate" =>	"dpFinishDate",
			"active"	 =>	"dpActive",
			"descr"		 =>	"dpDescription",
			"childprice" =>	"dpChildPrice",
			"adultprice" =>	"dpAdultPrice",
            "affiliate"  => "dpAffiliateID"  
		);

	BusinessObject::setAttributes();
  }
  /* constuctor */
  function DailyPricesBO(){
    /** super(tableName) */
    BusinessObject::BusinessObject("item_dailyprices");
    $this->setAttributes();
  }

  function checkDiscount($start,$finish,$id="",$item) //return true if all ok with date ranges.
  {
    $sd = new DailyPricesBO();
	  $where = "('".$start."' <= ".$sd->fields["finishdate"]." and '".$start."' >= ".$sd->fields["startdate"].") OR ".
    "('".$finish."' <= ".$sd->fields["finishdate"]." and '".$finish."' >= ".$sd->fields["startdate"].") OR ".
    "('".$finish."' >= ".$sd->fields["finishdate"]." and '".$start."' <= ".$sd->fields["startdate"].")";
    $where = "(".$where.") and ".$sd->fields["item"]." = ".$item;
	  if ($id != "")	$where = "(".$where.") and ".$sd->fields["id"]." <> ".$id;

	  $sd->Load(0,0,"",$where);
	  if($sd->Next()) {
			return false;
      }
    //not exist specials in the same date range - return TRUE;
    return(true);
  }

  function returnPrice ($date,$item, $affiliateID="0") {
	$sd = new DailyPricesBO();
	$where = "('".$date."' <= ".$sd->fields["finishdate"]." and '".$date."' >= ".$sd->fields["startdate"].") AND (".$sd->fields["affiliate"]."=".$affiliateID." OR ".$sd->fields["affiliate"]."=0)";
	$sd->item = $item->id;
	$res = Array();
	$sd->load(0,0,"",$where);
	if (!$sd->next()) {
		$res[0] = $item->adult_price;
		$res[1] = $item->child_price;
	} else {
		$res[0] = $sd->adultprice;
		$res[1] = $sd->childprice;
	}
	return $res;
  }
}
?>
